﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class StoreDal
    {
        #region StoreDal private
        private static void SetInfo(IDataRecord reader, ref StoreEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<StoreEntity> advList)
        {
            while (reader.Read())
            {
                var o = new StoreEntity();
                SetInfo(reader, ref o);
                advList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<StoreEntity> GetListByTypeId(int typeId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstStore = new List<StoreEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Store_GetByType", typeId);
                SetInfoList(ref reader, ref lstStore);
                return lstStore;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Adv_GetListByTypeId: {0}", ex.Message));
            }
        }

    }
}
